package com.siemens.industrial.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/10/13.
 */
public enum Definition {
    SERVICE("/service.json"),EVENT("/event.json"),STATUSTYPE("/statusType.json");

    private static final ObjectMapper objectMapper = new ObjectMapper();
    private String path;

    Definition(String path) {
        this.path = path;
    }

    public List getData() {
        try {
            InputStream inputStream = getClass().getResourceAsStream(path);
            return objectMapper.readValue(inputStream, List.class);
        } catch (IOException e) {
            return new ArrayList<>();
        }
    }

    public Map getData(int id) {
        List items = getData();

        for (Object o : items) {
            if (!(o instanceof Map))
                continue;

            Map oMap = (Map) o;

            if (oMap.containsKey("id") && oMap.get("id") instanceof Integer && (int) oMap.get("id") == id)
                return oMap;
        }

        return null;
    }

}
